#include "DataTransportConfig.hh"

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>

#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>

DataTransportConfig::DataTransportConfig() {
	//int k=t;
}
DataTransportConfig::~DataTransportConfig() {
	//
}
void DataTransportConfig::error(char *msg) {
	perror(msg);
	exit(0);
}
void DataTransportConfig::Configure() {
	//NumOfChan = param.Get("NumOfChan", 1280);
	//NumOfADC = param.Get("NumOfADC", 2);
	for (unsigned i = 2; i < 6; ++i) {
		//unsigned addr = param.Get("Mimosa_" + to_string(i), 0);
		//std::cout << "mi " <<  addr  << std::endl;
	}
}
void DataTransportConfig::GetProduserHostInfo() {
	/*** get Producer information, NAME and INET ADDRESS ***/
	gethostname(ThisHost, MAXHOSTNAME);
	printf("----TCP/Producer running at host NAME: %s\n", ThisHost);
	hclient = gethostbyname(ThisHost);
	bcopy(hclient->h_addr, &(client.sin_addr), hclient->h_length);
	printf("----TCP/Producer INET ADDRESS is: %s \n", inet_ntoa(client.sin_addr));

}
void DataTransportConfig::ClientConfigSocketRun() {
	/*** Network configuration for NI, NAME and INET ADDRESS ***/
	data = inet_addr("192.76.172.185");
	hconfig = gethostbyaddr(&data, 4, AF_INET);
	if (hconfig == NULL) {
		perror("gethostbyname()");
		exit(1);
	} else {
		bcopy(hconfig->h_addr, &(config.sin_addr), hconfig->h_length);
		printf("----TCP/NI crate INET ADDRESS is: %s \n", inet_ntoa(config.sin_addr));
		printf("----TCP/NI crate INET PORT is: %d \n", PORT_CONFIG);
	}
	if ((sock_config = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
		perror("socket()");
		exit(1);
	} else
		printf("----TCP/NI crate: The SOCKET is OK...\n");

	config.sin_family = AF_INET;
	config.sin_port = htons(PORT_CONFIG);
	memset(&(config.sin_zero), '\0', 8);
	if (connect(sock_config, (struct sockaddr *) &config, sizeof(struct sockaddr)) == -1) {
		perror("connect()");
		exit(1);
	} else
		printf("----TCP/NI crate: The CONNECT is OK...\n");
}
void DataTransportConfig::ClientDatatransportSocketRun() {
	/*** Creation for the data transmit socket, NAME and INET ADDRESS ***/
	data_trans_addres = inet_addr("192.76.172.185");
	hdatatransport = gethostbyaddr(&data_trans_addres, 4, AF_INET);
	if (hdatatransport == NULL) {
		perror("gethostbyname()");
		exit(1);
	} else {
		bcopy(hdatatransport->h_addr, &(datatransport.sin_addr), hdatatransport->h_length);
		printf("----TCP/NI crate DATA TRANSPORT INET ADDRESS is: %s \n", inet_ntoa(datatransport.sin_addr));
		printf("----TCP/NI crate DATA TRANSPORT INET PORT is: %d \n", PORT_DATATRANSF);
	}
	if ((sock_datatransport = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
		perror("socket()");
		exit(1);
	} else
		printf("----TCP/NI crate DATA TRANSPORT: The SOCKET is OK...\n");
	//sleep (5);
	datatransport.sin_family = AF_INET;
	datatransport.sin_port = htons(PORT_DATATRANSF);
	memset(&(datatransport.sin_zero), '\0', 8);
	if (connect(sock_datatransport, (struct sockaddr *) &datatransport, sizeof(struct sockaddr)) == -1) {
		perror("connect()");
		exit(1);
	} else
		printf("----TCP/NI crate DATA TRANSPORT: The CONNECT is OK...\n");
}
unsigned int DataTransportConfig::ReadDataTransportSocket_length(const char string[4]) {
	unsigned int datalengthTmp;
	unsigned int datalength;
	if ((numbytes = recv(sock_datatransport, Buffer_length, 2, 0)) == -1) {
		perror("recv()");
		exit(1);
	}
	datalengthTmp = 0;
	datalengthTmp = 0xFF & Buffer_length[0];
	datalengthTmp <<= 8;
	datalengthTmp += 0xFF & Buffer_length[1];
	datalength = datalengthTmp;

	return datalength;
}
unsigned int DataTransportConfig::ReadConfigSocket_length(const char string[4]) {
	unsigned char *Buffer_length = new unsigned char;
	unsigned int datalengthTmp;
	unsigned int datalength;
	if ((numbytes = recv(sock_config, Buffer_length, 2, 0)) == -1) {
		perror("recv()");
		exit(1);
	}
	datalengthTmp = 0;
	datalengthTmp = 0xFF & Buffer_length[0];
	datalengthTmp <<= 8;
	datalengthTmp += 0xFF & Buffer_length[1];
	datalength = datalengthTmp;
	printf("dataLengthTmp = %d  \n", datalengthTmp);

	return datalength;
}
char *DataTransportConfig::ReadDataTransportSocket_data(int datalength) {
	if ((numbytes = recv(sock_datatransport, Buffer_data, datalength, 0)) == -1) {
		perror("recv()");
		exit(1);
	}

	return Buffer_data;
}
unsigned char *DataTransportConfig::ReadConfigSocket_data(int datalength) {
	unsigned char *Buffer_data = new unsigned char;
	if ((numbytes = recv(sock_config, Buffer_data, datalength, 0)) == -1) {
		perror("recv()");
		exit(1);
	}
	printf("data0 %d=", Buffer_data[5]);

	return Buffer_data;
}
unsigned char* DataTransportConfig::ReadClientConfigSocket(const char string[4]) {
	unsigned char *Buffer = new unsigned char;
	int datalengthTmp;
	int datalength;

	if ((numbytes = recv(sock_config, Buffer, 2, 0)) == -1) {
		perror("recv()");
		exit(1);
	} else
		printf("Client-The recv() is OK...\n");

	datalengthTmp = 0;
	datalengthTmp = 0xFF & Buffer[0];
	datalengthTmp <<= 8;
	datalengthTmp += 0xFF & Buffer[1];
	datalength = datalengthTmp;

	if ((numbytes = recv(sock_config, Buffer, datalength, 0)) == -1) {
		perror("recv()");
		exit(1);
	} else
		printf("Client-The recv() is OK...\n");
	return Buffer;
}
void DataTransportConfig::SendClientConfigSocket(const char *text) {
	if (send(sock_config, text, 4, 0) == -1)
		perror("Server-send() error lol!");
	printf("STOP \n");
}

